// shop/detail/detail.js
import { GoodTypeConfig, getGoodsInfo, exchangeGoodReq } from '@/api/shop'
import { showText } from '../../utils/util';
let timeOut = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    bottomInset: 20,
    swiperIndex: 0,
    swiperDate: [],
    data: {},
    source: 1, // 数据来源 1 积分商城（默认） 2 甄选
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!options.id) {
      showText("商品有误");
      return;
    } else this.goodsId = options.id;

    if (options.source) {
      this.setData({
        source: options.source
      })
    }
    getGoodsInfo(options.id).then(res => {
      console.log(res)
      if (res.code == 200) {
        let description = res.data.description
        if (description.indexOf('[') != -1) {
          res.data.description = JSON.parse(description);
        } else {
          res.data.description = description.split(',');
        }
        console.log(res.data.description);
        let { data } = res
        let swiperDate = data.imgUrlList.split(',')
        this.setData({
          data: data,
          swiperDate
        })
      }
    })
  },

  startExchange() {
    const good = this.data.data;
    if (good.goodsType == GoodTypeConfig.virtual) {
      exchangeGoodReq({ goodsId: this.goodsId }).then(res => {
        if (res.code == 200) {
          wx.showModal({
            title: "兑换成功",
            showCancel: false,
            success: () => this.toGoodPage()
          })

        } else showText(res.msg);
      });

    } else this.toGoodPage();
  },

  toGoodPage() {
    const good = this.data.data;
    if (good.jumpType == "INNER_APP") {//内部小程序
      wx.reLaunch({
        url: '/' + good.jumpParam,
      });
    } else if (good.jumpType == "OUT_APP") {//外部小程序
      wx.navigateToMiniProgram({
        appId: good.jumpAppid,
        path: good.jumpParam
      });
    } else if (good.jumpType == "WEB_H5") {//
      wx.navigateTo({
        url: '/shop/webview/webview?webSrc=' + good.jumpParam,
      })
    }
  },
  swiperChange(e) {
    clearTimeout(timeOut);
    this.setData({
      swiperIndex: e.detail.current
    })
    timeOut = setTimeout(() => {
      this.nextSwiper();
    }, 3000)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})